/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 音乐 UI 组件单元测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:music_example/viewmodels/music_player_view_model.dart';
import 'package:music_example/widgets/music_widgets/index.dart';

void main() {
  group('音乐 UI 组件测试', () {
    late MusicPlayerViewModel viewModel;

    setUp(() {
      viewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      viewModel.dispose();
    });

    Widget createTestWidget(Widget child) {
      return MaterialApp(
        home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
          value: viewModel,
          child: Scaffold(body: child),
        ),
      );
    }

    testWidgets('PlayOrPauseWidget 应该显示正确的图标', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const PlayOrPauseWidget()));

      // 初始状态应该显示播放图标
      expect(find.byIcon(Icons.play_arrow), findsOneWidget);
      expect(find.byIcon(Icons.pause), findsNothing);
    });

    testWidgets('SongInfoWidget 应该显示空状态', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const SongInfoWidget()));

      // 应该显示空状态文本
      expect(find.text('暂无播放内容'), findsOneWidget);
      expect(find.text('请选择音乐开始播放'), findsOneWidget);
    });

    testWidgets('AlbumCoverWidget 应该显示占位符', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const AlbumCoverWidget()));

      // 应该显示音乐图标作为占位符
      expect(find.byIcon(Icons.music_note), findsOneWidget);
    });

    testWidgets('PlayModeWidget 应该显示默认播放模式', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const PlayModeWidget()));

      // 默认应该是列表循环模式
      expect(find.byIcon(Icons.repeat_rounded), findsOneWidget);
    });

    testWidgets('ProgressSliderWidget 应该显示进度条', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const ProgressSliderWidget()));

      // 应该显示滑动条和时间标签
      expect(find.byType(Slider), findsOneWidget);
      expect(find.text('00:00'), findsNWidgets(2)); // 开始和结束时间都是 00:00
    });

    testWidgets('PlaybackControlsWidget 应该显示所有控制按钮',
        (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const PlaybackControlsWidget()));

      // 应该显示所有控制按钮
      expect(find.byIcon(Icons.skip_previous), findsOneWidget);
      expect(find.byIcon(Icons.play_arrow), findsOneWidget);
      expect(find.byIcon(Icons.skip_next), findsOneWidget);
      expect(find.byIcon(Icons.repeat_rounded), findsOneWidget); // 播放模式按钮
      expect(find.byIcon(Icons.queue_music), findsOneWidget); // 播放列表按钮
    });

    testWidgets('CompactSongInfoWidget 应该显示紧凑格式', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const CompactSongInfoWidget()));

      // 应该显示空状态文本
      expect(find.text('暂无播放内容'), findsOneWidget);
    });

    testWidgets('CompactPlaybackControlsWidget 应该显示紧凑控制按钮',
        (WidgetTester tester) async {
      await tester
          .pumpWidget(createTestWidget(const CompactPlaybackControlsWidget()));

      // 应该显示紧凑版控制按钮
      expect(find.byIcon(Icons.skip_previous), findsOneWidget);
      expect(find.byIcon(Icons.play_arrow), findsOneWidget);
      expect(find.byIcon(Icons.skip_next), findsOneWidget);
    });
  });

  group('音乐 UI 组件交互测试', () {
    late MusicPlayerViewModel viewModel;

    setUp(() {
      viewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      viewModel.dispose();
    });

    Widget createTestWidget(Widget child) {
      return MaterialApp(
        home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
          value: viewModel,
          child: Scaffold(body: child),
        ),
      );
    }

    testWidgets('点击播放模式按钮应该切换模式', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget(const PlayModeWidget()));

      // 初始状态是列表循环
      expect(find.byIcon(Icons.repeat_rounded), findsOneWidget);

      // 点击按钮
      await tester.tap(find.byType(IconButton));
      await tester.pump();

      // 应该切换到单曲循环
      expect(find.byIcon(Icons.repeat_one_rounded), findsOneWidget);
    });
  });
}
